/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package es.unavarra.mti.iws.logic.undoredo;

import es.unavarra.mti.iws.logic.editor.DefaultEditor;
import es.unavarra.mti.iws.logic.editor.Rotable;
import es.unavarra.mti.iws.logic.editor.Scalable;
import es.unavarra.mti.iws.logic.editor.Selectable;
import java.util.ArrayList;

/**
 *
 * @author Asier
 */
public class ModifySelectable implements UndoRedoOperation{
    private int usePattern;
    private final Selectable[] selectables;
    private float[] oldX,oldY;
    private float[] oldRot;
    private float[] oldScaleX,oldScaleY;
    private float[] newX,newY;
    private float[] newRot;
    private float[] newScaleX,newScaleY;

    public ModifySelectable(ArrayList<Selectable> objects, int pattern, 
            float[] x, float[] y, float[] rot,float[] scaleX, float[] scaleY){
        if(objects == null || objects.isEmpty()) {selectables = null; return;}
        synchronized(objects){
            usePattern = pattern;
            int size = objects.size();
            selectables = new Selectable[size];
            if ((usePattern & DefaultEditor.USE_POSITION) != 0) {oldX = x; newX = new float[size]; oldY = y; newY = new float[size];}
            if ((usePattern & DefaultEditor.USE_ROT) != 0) {oldRot = rot; newRot = new float[size];}
            if ((usePattern & DefaultEditor.USE_SCALEX) != 0) {oldScaleX = scaleX; newScaleX = new float[size];}
            if ((usePattern & DefaultEditor.USE_SCALEY) != 0) {oldScaleY = scaleY; newScaleY = new float[size];}
            int index = 0;
            for(Selectable s : objects){
                selectables[index] = s;
                if ((usePattern & DefaultEditor.USE_POSITION) != 0) {newX[index] = s.getX();newY[index] = s.getY();}
                if ((usePattern & DefaultEditor.USE_ROT) != 0 && (s instanceof Rotable))
                    {newRot[index] = ((Rotable)s).getRotation();}
                if ((usePattern & DefaultEditor.USE_SCALEX) != 0 && (s instanceof Scalable))
                    {newScaleX[index] = ((Scalable)s).getScaleX();}
                if ((usePattern & DefaultEditor.USE_SCALEY) != 0 && (s instanceof Scalable))
                    {newScaleY[index] = ((Scalable)s).getScaleY();}
                index++;
            }
        }
    }

    public void redo() {
        if(selectables == null){return; }
        synchronized(selectables){
            int size = selectables.length;
            for(int i = 0; i < size; i++){
                if ((usePattern & DefaultEditor.USE_POSITION) != 0) {selectables[i].setPosition( newX[i],newY[i] ); }
                if ((usePattern & DefaultEditor.USE_ROT) != 0 && (selectables[i] instanceof Rotable))
                    {((Rotable)selectables[i]).setRotation( newRot[i] ); }
                if ((usePattern & DefaultEditor.USE_SCALEX) != 0 && (selectables[i] instanceof Scalable))
                    {((Scalable)selectables[i]).setScaleX( newScaleX[i] ); }
                if ((usePattern & DefaultEditor.USE_SCALEY) != 0 && (selectables[i] instanceof Scalable))
                    {((Scalable)selectables[i]).setScaleY( newScaleY[i] ); }
            }
        }
    }


    public void undo() {
       if(selectables == null){return; }
        synchronized(selectables){
            int size = selectables.length;
            for(int i = size-1; i >= 0; i--){
                if ((usePattern & DefaultEditor.USE_POSITION) != 0) {selectables[i].setPosition( oldX[i],oldY[i] ); }
                if ((usePattern & DefaultEditor.USE_ROT) != 0 && (selectables[i] instanceof Rotable))
                    {((Rotable)selectables[i]).setRotation( oldRot[i] ); }
                if ((usePattern & DefaultEditor.USE_SCALEX) != 0 && (selectables[i] instanceof Scalable))
                    {((Scalable)selectables[i]).setScaleX( oldScaleX[i] ); }
                if ((usePattern & DefaultEditor.USE_SCALEY) != 0 && (selectables[i] instanceof Scalable))
                    {((Scalable)selectables[i]).setScaleY( oldScaleY[i] ); }
            }
        }
    }

    

}
